﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Streambolics.PatLite
{
    /// <summary>
    ///     The status of a PHC-100 device
    /// </summary>
    /// <remarks><para>
    ///     A PHC-100 is simply a 8-way output device.
    /// </para></remarks>

    public class PhcStatus
    {
        private Ternary[] _Outputs = new Ternary[8];

        public event EventHandler Changing;
        public event EventHandler Changed;

        /// <summary>
        ///     Create a PhcStatus in unknown state.
        /// </summary>

        public PhcStatus ()
        {
            SetUnknown ();
        }

        /// <summary>
        ///     Create a PhcStatus in the same state as another
        ///     one.
        /// </summary>
        /// <param name="aOther"></param>

        public PhcStatus (PhcStatus aOther)
        {
            Set (aOther);
        }

        private void DoChanging ()
        {
            OnChanging (EventArgs.Empty);
        }

        private void DoChanged ()
        {
            OnChanged (EventArgs.Empty);
        }

        /// <summary>
        ///     Called when the status is about to change.
        /// </summary>
        /// <param name="e"></param>

        protected virtual void OnChanging (EventArgs e)
        {
            if (Changing != null)
            {
                Changing (this, e);
            }
        }

        protected virtual void OnChanged (EventArgs e)
        {
            if (Changed != null)
            {
                Changed (this, e);
            }
        }

        /// <summary>
        ///     The state of the i-th output.
        /// </summary>
        /// <param name="i">
        ///     The index of the output. Outputs are numbered from
        ///     0 to 7.
        /// </param>
        /// <returns>
        ///     The status of the corresponding output.
        /// </returns>

        public Ternary this[int i]
        {
            get
            {
                return _Outputs[i];
            }
            set
            {
                if (_Outputs[i] != value)
                {
                    DoChanging ();
                    _Outputs[i] = value;
                    DoChanged ();
                }
            }
        }

        /// <summary>
        ///     Sets all outputs to the unknown state.
        /// </summary>

        public void SetUnknown ()
        {
            for (int i = 0; i < 8; i++)
            {
                _Outputs[i] = Ternary.Unknown;
            }    
        }

        /// <summary>
        ///     Copies the status from another.
        /// </summary>
        /// <param name="aOther">
        ///     The status to copy.
        /// </param>

        public void Set (PhcStatus aOther)
        {
            for (int i = 0; i < 8; i++)
            {
                _Outputs[i] = aOther._Outputs[i];
            }
        }

        /// <summary>
        ///     Compares two statuses.
        /// </summary>
        /// <param name="aOther">
        ///     The other status to compare.
        /// </param>
        /// <returns>
        ///     True if the statuses are the same, false otherwise.
        ///     Not a true equality, can return "unknown".
        /// </returns>

        public Ternary SameAs (PhcStatus aOther)
        {
            Ternary r = Ternary.True;
            for (int i = 0; i < 8; i++)
            {
                if (_Outputs[i] != aOther._Outputs[i])
                {
                    return Ternary.False;
                }
                else if (_Outputs[i] == Ternary.Unknown)
                {
                    r = Ternary.Unknown;
                }
            }
            return r;
        }
    }
}
